package com.devilvan.betrice.util;

import cn.hutool.extra.servlet.ServletUtil;
import cn.hutool.http.useragent.UserAgent;
import cn.hutool.http.useragent.UserAgentUtil;
import com.devilvan.betrice.constant.CommonConstant;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Evad.Wu
 * @Description 客户端 工具类
 * @date 2023-08-13
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class UServlet extends ServletUtil {
    /**
     * 获取请求方使用的浏览器
     *
     * @return 请求方使用的浏览器
     */
    public static String getBrowser() {
        UserAgent userAgent = getRequestUserAgent();
        if (userAgent != null) {
            if (userAgent.getOs() != null) {
                return userAgent.getBrowser().toString();
            }
        }
        return null;
    }

    /**
     * 获取请求方的操作系统
     *
     * @return 请求方的操作系统
     */
    public static String getOs() {
        UserAgent userAgent = getRequestUserAgent();
        if (userAgent != null) {
            if (userAgent.getOs() != null) {
                return userAgent.getOs().toString();
            }
        }
        return null;
    }

    /**
     * 获取当前请求头中的User-Agent
     *
     * @return UserAgent对象
     */
    public static UserAgent getRequestUserAgent() {
        if (getRequest() != null) {
            return UserAgentUtil.parse(getRequest().getHeader(CommonConstant.USER_AGENT));
        }
        return null;
    }

    /**
     * 获取发起请求的IP地址
     *
     * @return 发起请求的IP地址
     */
    public static String getInitiatedRequestIp() {
        if (getRequest() != null) {
            return getClientIP(getRequest());
        }
        return null;
    }

    /**
     * 获取请求的URL
     *
     * @return 请求的URL
     */
    public static String getRequestUrl() {
        if (getRequest() != null) {
            if (getRequest().getRequestURL() != null) {
                return getRequest().getRequestURL().toString();
            }
        }
        return null;
    }

    /**
     * 获取httpResponse对象
     *
     * @return httpResponse对象
     */
    public static HttpServletResponse getResponse() {
        if (getRequestAttributes() != null) {
            return getRequestAttributes().getResponse();
        }
        return null;
    }

    /**
     * 获取httpRequest对象
     *
     * @return httpRequest对象
     */
    public static HttpServletRequest getRequest() {
        if (getRequestAttributes() != null) {
            return getRequestAttributes().getRequest();
        }
        return null;
    }

    /**
     * 获取客户端请求参数集对象
     *
     * @return 请求参数集对象
     */
    public static ServletRequestAttributes getRequestAttributes() {
        RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
        if (attributes != null) {
            return (ServletRequestAttributes) attributes;
        }
        return null;
    }
}
